package newPan3d.content.lightScene
{
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Focus3D;
	import _Pan3D.base.Object3D;
	import _Pan3D.core.MathCore;
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.role.Npc;
	
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import editAir.lightScene.PerlinDisplay3DSprite;
	import editAir.lightScene.PerlinDisplayShader;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Program3D;
	
	import newPan3d.content.npc.NpcLevel;

	public class LightMenager
	{
		private var _display3DContainer:Display3DContainer;
		private var _context3D:Context3D;

		private var _frieDisplay3DContainer:Display3DContainer;
		private var _lightDisplay3DFrieSprite:LightDisplay3DFrieSprite;
		private var selfShaderdisplay3DSprite:LightDisplay3DSprite;
		private var _backLightDisplay3DSprite:BackLightDisplay3DSprite;
		private var _lightDisplay3DSprite:LightDisplay3DSprite;
		private var _erlinDisplay3DSprite:PerlinDisplay3DSprite;

		public function LightMenager()
		{
			_display3DContainer=new Display3DContainer();
			_context3D=Scene_data.context3D;
		
			addBackLightSprite();
			addChooseGuanwuQuan();
			addSelfShader();
			
			addPerLinShader();

			var treeShaderProgram3d:Program3D=Program3DManager.getInstance().getProgram(LightDisplayShader.LIGHT_DISPLAY_SHADER);
			_display3DContainer.setProgram3D(treeShaderProgram3d);
			
			
			addFries();
		}
		
		private function addPerLinShader():void
		{
			//加载柏林噪音
			_erlinDisplay3DSprite =new PerlinDisplay3DSprite(_context3D);
			_erlinDisplay3DSprite.url ="guangquan";
			_erlinDisplay3DSprite.x=0;
			_erlinDisplay3DSprite.y=1;
			_erlinDisplay3DSprite.z=0;
			_erlinDisplay3DSprite.scale=1;
		//	_display3DContainer.addChild(_erlinDisplay3DSprite);
			
			var treeShaderProgram3d:Program3D=Program3DManager.getInstance().getProgram(PerlinDisplayShader.PERLIN_DISPAY_SHADER);
			_erlinDisplay3DSprite.newprogram=treeShaderProgram3d;

			
		}
		private function addFries():void
		{
			_frieDisplay3DContainer=new Display3DContainer();
			
			addFrieShader();
			
			var treeShaderProgram3d:Program3D=Program3DManager.getInstance().getProgram(LightDisplayFrieShader.LIGHT_DISPLAY_FRIE_SHADER);
			_frieDisplay3DContainer.setProgram3D(treeShaderProgram3d);
			
		
			
		}		
		
		private function addFrieShader():void
		{
			 _lightDisplay3DFrieSprite =new LightDisplay3DFrieSprite(_context3D);
			 _lightDisplay3DFrieSprite.url ="frieshaderpic128";
			 _lightDisplay3DFrieSprite.x=100;
			 _lightDisplay3DFrieSprite.y=1;
			 _lightDisplay3DFrieSprite.z=0;
			_lightDisplay3DFrieSprite.scale=1;
	
			_frieDisplay3DContainer.addChild(_lightDisplay3DFrieSprite);
		}
		
		private function addSelfShader():void
		{
			selfShaderdisplay3DSprite =new LightDisplay3DSprite(_context3D);
			selfShaderdisplay3DSprite.url ="selfshaderpic128";
			selfShaderdisplay3DSprite.x=0;
			selfShaderdisplay3DSprite.y=1;
			selfShaderdisplay3DSprite.z=0;
			selfShaderdisplay3DSprite.scale=0.5;

			_display3DContainer.addChild(selfShaderdisplay3DSprite);
			
			
		
			
			
		}
		private function addBackLightSprite():void
		{
			_backLightDisplay3DSprite=new BackLightDisplay3DSprite(_context3D);
			_backLightDisplay3DSprite.url="guangquan";
			_backLightDisplay3DSprite.x=0;
			_backLightDisplay3DSprite.y=0;
			_backLightDisplay3DSprite.z=0;
			_backLightDisplay3DSprite.scale=1;
	
			_display3DContainer.addChild(_backLightDisplay3DSprite);
		}

	
		private function addChooseGuanwuQuan():void
		{
			_lightDisplay3DSprite =new LightDisplay3DSprite(_context3D);
			_lightDisplay3DSprite.url ="guangquan";
			_lightDisplay3DSprite.x=0;
			_lightDisplay3DSprite.y=10;
			_lightDisplay3DSprite.z=0;
			_lightDisplay3DSprite.scale=0.2;
			_display3DContainer.addChild(_lightDisplay3DSprite);
			
			
		}
		
		public function updata(lookAt:Boolean=false):void
		{
			//先复制作镜头。。
			var tempCam3D:Camera3D=Scene_data.cam3D.clone();
			var fous3D:Focus3D=Scene_data.focus3D.cloneFocus3D();

			Scene_data.cam3D.distance=1024;
			Scene_data.cam3D.fovw=1024;
			Scene_data.cam3D.fovh=1024;
			fous3D.x=0
			fous3D.y=0
			fous3D.z=0
			fous3D.angle_x=-90;
			fous3D.angle_y=0;
		

			MathCore._catch_cam(Scene_data.cam3D, fous3D);


			if (!lookAt)
			{
				_context3D.setRenderToTexture(Scene_data.groundLightText, false, 0, 0);
			}
			_context3D.clear(0, 0, 0, 0);

			var _projMatrix:PerspectiveMatrix3D=new PerspectiveMatrix3D();
			_projMatrix.perspectiveFieldOfViewLH(1, 1, 0.1, 3000);
			_context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, _projMatrix, true);

			_context3D.setCulling(Context3DTriangleFace.NONE);
			_context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			_context3D.setDepthTest(false, Context3DCompareMode.LESS);


			
			var focus3D:Focus3D=Scene_data.focus3D;
			var pos:Object3D=new Object3D(500, 0, 500);

			if (Scene_data.self3DMove && Scene_data.self3DMove.target)
			{
				pos.x=Scene_data.self3DMove.target.x
				pos.z=Scene_data.self3DMove.target.z
				_lightDisplay3DSprite.x=(pos.x-focus3D.x)*2.20
				_lightDisplay3DSprite.z=(pos.z-focus3D.z)*2.20
				_lightDisplay3DSprite.rotationY=_lightDisplay3DSprite.rotationY+20
			}else{
				_lightDisplay3DSprite.x=1000
				_lightDisplay3DSprite.z=0
			}
			if(_backLightDisplay3DSprite){
			_backLightDisplay3DSprite.rotationY=-Scene_data.focus3D.angle_y;
			}
			_frieDisplay3DContainer.update();
			
			selfShaderdisplay3DSprite.scale=0.5;
			selfShaderdisplay3DSprite.x=0;
			selfShaderdisplay3DSprite.y=1;
			selfShaderdisplay3DSprite.z=0;
			
			_display3DContainer.update();
			
			setNpcShaders();
			
			var friepos:Object3D=new Object3D(430, 0, 400);
			
			if(_lightDisplay3DFrieSprite){
				
				_lightDisplay3DFrieSprite.x=(friepos.x-focus3D.x)*2.20
				_lightDisplay3DFrieSprite.z=(friepos.z-focus3D.z)*2.20
	
			}
		

			if (lookAt)
			{
				_context3D.present();
			}
			else
			{
				_context3D.setRenderToBackBuffer();
			}
			Scene_data.cam3D=tempCam3D;
			Scene_data.sceneLightText=Scene_data.groundLightText;

		}
		private function setNpcShaders():void
		{
			var focus3D:Focus3D=Scene_data.focus3D;
			for each(var npc:Npc in NpcLevel.npcItem){
			
		        if(npc.inRender){
				selfShaderdisplay3DSprite.scale=0.2;
				selfShaderdisplay3DSprite.x=(npc.x-focus3D.x)*2.20
				selfShaderdisplay3DSprite.z=(npc.z-focus3D.z)*2.20

				selfShaderdisplay3DSprite.update();
				}
			}
		}
	}
}
